home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / dviware / dvi2xx / lj3-filter < prev    next >
Text File  |  1994-04-24  |  3KB  |  117 lines

  1. #!/usr/local/bin/perl
  2. #####################################################################
  3. #  Stefan Esser
  4. #
  5. # /usr/local/lib/lpdfilters/lj3-filter
  6. #
  7. #    BSD line printer filter for HP-PCL 5 printer
  8. #      with independent init and uninit strings 
  9. #      for HP-PCL and HP-GL modes
  10. #
  11. #       Fri Jan  3 12:39:43 1992 -- Gustaf Neumann
  12. #       added support for printing of dvi-files 
  13. #
  14. # CONFIGURATION:
  15. #  - the location of perl (headerline)             /usr/local/bin/perl
  16. #  - the location of the tmp file (variable $tmpfile below)
  17. #  - the name of the dvi-filter and its options (variable $dvifilter below)
  18.  
  19. $tmpfile = "/usr/tmp/dvilj$$.dvi";
  20. $dvifilter = "/usr/local/bin/dvilj -q -s26 -e- $tmpfile";
  21.  
  22. #--------------------------------------------------------------------
  23. # init and uninit strings for HP-PCL and HP-GL modes
  24. #
  25. $text_inistr = "\033E\033&k2G";        # Text init string
  26. $text_unistr = "\033E";            # Text un_init string
  27.  
  28. #$hpgl_inistr = "\033E\033%0B";        # HP-GL init string
  29. $hpgl_inistr = "\033E\033&l1O\033%0B";    # HP-GL init string (rot 90 degree)
  30. $hpgl_unistr = "\033E";            # HP-GL un_init string
  31. $stdpwcmd    = "PW 0.15;";        # default pen width in HP-GL mode
  32.  
  33. #--------------------------------------------------------------------
  34. # describe (a superset of :-) the HP-GL commands
  35. #
  36. $hpglcmd = '(SM.|LB[^\003]*\003|[A-Z][A-Z][-\d,.\s]*|[.,;]|\033\..([\d;\s]*:)?|\s)';
  37.  
  38. #--------------------------------------------------------------------
  39. # lprm sends SIGINT (is THIS signal handler necessary ???)
  40. #
  41. $SIG{'INT'}  = 'CLEANUP';
  42.  
  43. sub CLEANUP {
  44.     print ($hpglfile ? $hpgl_unistr : $text_unistr);
  45.     unlink $tmpfile if $dvifile;
  46.     close(STDOUT);
  47.     exit 2;
  48. }
  49.  
  50. #--------------------------------------------------------------------
  51. # read enough (=4KByte) data to choose HP-GL or HP-PCL mode
  52. #
  53. $numread = read(STDIN,$buffer,4096);
  54.  
  55. #--------------------------------------------------------------------
  56. # check whether the file is a dvi-file
  57.  
  58. $dvifile = $buffer =~ m/^\367\002/oi;
  59. if ($dvifile) {
  60.     open(SPOOL,">$tmpfile") || die "Can't open $tmpfile for writing!";
  61.     while ($numread > 0) {
  62.     print SPOOL  $buffer;
  63.     $numread=read(STDIN,$buffer,4096);
  64.     }
  65.     close(SPOOL) || exit (1);
  66.     system $dvifilter || exit (1);
  67.     unlink $tmpfile;
  68.     close(STDOUT) || exit (1);
  69.     exit (0);
  70. }
  71.  
  72.  
  73. #--------------------------------------------------------------------
  74. # look for a sequence of HP-GL commands
  75. #
  76. $buffer  =~ m/^$hpglcmd+/oi;
  77.  
  78. $matchlen= length($&);
  79. $hpglfile= $matchlen > 10 && $matchlen / $numread > 0.95;
  80.  
  81. #--------------------------------------------------------------------
  82. # add command to set pen width after 'IN', if found within the first 20 bytes
  83. # else that default pen width
  84. #
  85. if ($hpglfile && substr($buffer,0,20) =~ m/IN/i) {
  86.     $pwcmd  = ($buffer =~ m/(PW\s*\d+\.?\d*\s*;)/i)[0] || $stdpwcmd;
  87.     $buffer =~ s/IN\s*;/IN;$pwcmd/i;
  88. }
  89.  
  90.  
  91. #--------------------------------------------------------------------
  92. # print init string, all data, uninit string to STDOUT
  93. #
  94. print ($hpglfile ? $hpgl_inistr : $text_inistr);
  95.  
  96. while ($numread > 0) {
  97.     print $buffer;
  98.     $numread=read(STDIN,$buffer,4096);
  99. }
  100.  
  101. print ($hpglfile ? $hpgl_unistr : $text_unistr);
  102.  
  103. close(STDOUT) || exit (1);
  104.  
  105. exit (0);
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.